/**
 * 活动用户列表页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：活动用户列表页面
     创建日期：2015-10-12
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	qrPromoteAcUser();
	 $(".date").datepicker({
	        todayBtn: "linked",
	        keyboardNavigation: false,
	        forceParse: false,
	        calendarWeeks: true,
	        autoclose: true
	    });
});

/**
 * 查询活动用户列表
 * @returns
 */
var qrPromoteAcUser = function(){
	
    var number=$("#s_number").val();
    var areaId = $("#areaId").val();
    var userID=$("#userID").val();
    var nickName=$("#nickName").val();		
    var registTime=$("#registTime").val();	
    var s_status=$("#auditStatusList").val();
    
    if(userID)
    {
    	userID = userID.trim();
    }
    else
    {
    	userID = 0;
    }	
    
	
	 var datas =  {"p":"seachAgentDevelopUsers","m":"queryAgentDevelopUsers","agentId":number,"userId":userID,"areaId":areaId,"userName":nickName,"createTime":registTime,"auditStatus":s_status};
	 var columnDefine = {"id":"ID","real_name":"用户昵称","type":"用户身份","regist_time":"关注时间","status":"审核状态"};
	 
	 var formatters={
	            "regist_time": function (column, row)
	            {
    				if(row.regist_time)
    				{
    					return timeFormatTrans(row.regist_time);
    				}
    				else
    				{
    					return "";
    				}
	            },
	            "type": function (column, rowData)
	            {
					var sf="";
					switch(rowData.type){
    					case 0:
    						sf="普通用户";
    						break;
    					case 1:
    						sf="快递员";
    						break;
    					case 2:
    						sf="送件快递员";
    						break;
    					case 3:
    						sf="站长";
    						break;
    					default:
    						break;
					};
					return sf;
	            },
				 "status": function (column, rowData )
				 {
    				if(rowData.status=0)
    				{
    					return "未审核";
    				}
    				else if(1)
    				{
    					return "审核通过";
    				}
    				else if(2)
    				{
    					return "审核未通过";
    				}
    				else
    				{
    					return "其他";
    				}
				 }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		buttons+='</p>';
	 hui.pager('service',datas ,"activeUserList", columnDefine , formatters , buttons ); //分页查询
	 
};

var getTheSureTime=function(emp){
	return emp>9?emp:"0"+emp;
}
//时间格式转换
var timeFormatTrans=function(time){
	
	if(time )
	{
		var newTime= new Date(time);
		var rowText=newTime.getFullYear()+'-'+getTheSureTime((newTime.getMonth()+1))+'-'
		+getTheSureTime(newTime.getDate())+' '+getTheSureTime(newTime.getHours())+':'
		+getTheSureTime(newTime.getMinutes())+':'+getTheSureTime(newTime.getSeconds());
		return rowText;
	}
	return '';
};


/**
 * 显示新增窗口
 * @returns
 */
var showNewWin = function(){
	$('#addWin input').val('');
	$('#addWin .userList').empty().hide();
	$('#addWin').modal('show');
	
};

/**
 * 显示批量添加窗口
 * @returns
 */
var showNewBathWin = function(){
	$('#batchAddWin textarea').val('');
	$('#batchAddWin').modal('show');
};

/**
 * 检索符合条件的用户
 * @returns
 */
var queryUsersByNameAndPhone = function(){
	var name = $('#addWin .name').val();
	var phone = $('#addWin .tel').val();
	var areaId = $('#areaId').val();
	
	var json = common.ajaxRequest('service?p=seachAgentDevelopUsers&m=queryUsersByNameAndPhone', {"name":name,"phone":phone,"areaId":areaId}, 'GET');
	
	if( json && json.result == 0 )
	{
		var html = '<hr>';
		html+='<div align="center">';
		html+='<table style="background-color: #acbecf;width:90%" cellspacing="1">';
		html+='<tr style="background-color: #acbecf;color:#fff">'
			html+='<th></th>'
		    html+='<th>姓名</th>'
			html+='<th>手机号</th>'
		html+='</tr>'
		if( json.userList )
		{
			$(json.userList).each(function(){
				 html+='<tr style="background-color: #fff">';
					 html+='<td>';
					 html+='<input type="checkbox" value="'+this.id+'" name="checkbox">';
					 html+='</td>';
					 html+='<td>';
					 html+=this.real_name;
					 html+='</td>';
					 html+='<td>';
					 html+= this.telephone ;
					 html+='</td>';
				 html+='</tr>';
			});
			
		}	
		html+='</table>';
		html+='</div>';
	}
	$('#addWin .userList').empty();
	$('#addWin .userList').append(html);
	$('#addWin .userList').show();
};

/**
 *添加快递员
 * @returns
 */
var addAgentUser = function(){
	
	var selectUsers =  $(".userList  input[name='checkbox']:checkbox");
	var str = '';
	var agentId = $('#s_number').val();
	var agentName = $('#s_agentName').val();
	var agentNumber = $('#s_no').val();
	
    $(selectUsers).each(function(){ 
        if($(this).is(':checked')){
            str += $(this).val()+","
        }
    });
    
    if( str!='' && str.indexOf(',')>0 )
    {
    	var ids = str.substring(0, str.lastIndexOf(','));
    	var datas = {"ids":ids,"agentId":agentId,"agentName":agentName,"agentNumber":agentNumber};
    	var json = common.ajaxRequest('service?p=seachAgentDevelopUsers&m=addAgentUsers', datas, 'GET');
    	if( json && json.result == 0 )
    	{
    		qrPromoteAcUser();
    		$('#addWin .userList').empty();
    		common.showMsg('info', '', '添加成功');
    	}
    	else
    	{
    		common.showMsg('error', '', '添加失败');
    		return;
    	}	
    }	
    else
    {
    	common.showMsg('error', '', '请选择快递员');
    	return;
    }	
    
};


/**
 * 批量添加快递员
 * @returns
 */
var addAgentUserBatch = function(){
	
	var data = $('#s_data').val();
	if( data==undefined || null == data || data == '' || data.indexOf(',')<=0 )
	{
		common.showMsg('error', '', '内容为空，或者内容格式错误');
		return;
	}	
	data = data.trim();
	var agentId = $('#s_number').val();
	
	var json = common.ajaxRequest('service?p=seachAgentDevelopUsers&m=addAgentUsersBatch', {"agentId":agentId,"data":data}, 'GET');
	if( json && json.result == 0 )
	{
		
		common.showMsg('info', '', '添加成功');
		qrPromoteAcUser();
		$('#batchAddWin').modal('hide');
		
	}
	else
	{
		common.showMsg('error', '', '添加失败');
		return;
	}	
};

